#include "stdafx.h"
#include "timemanager.h"
#include "systemtime.h"

namespace Clock
{
	TimeManager::TimeManager()
	{
		mySystemClock = 0;
		InitDefaults();
	}

	TimeManager::~TimeManager()
	{
		delete mySystemClock;
		mySystemClock = 0;
		while(myClocks.empty() == false)
		{
			delete myClocks[0];
			myClocks[0] = 0;
			myClocks.erase(myClocks.begin());
		}
	}

	void TimeManager::InitDefaults()
	{
		myDefaultClock = MASTER_CLOCK;
		delete mySystemClock;
		mySystemClock = new SystemTimer();
		while(myClocks.empty() == false)
		{
			delete myClocks[0];
			myClocks[0] = 0;
			myClocks.erase(myClocks.begin());
		}
		for(unsigned int timerIndex=SYSTEM_CLOCK; timerIndex<NUMBER_OF_CLOCKS; timerIndex++)
		{
			myClocks.push_back(static_cast<Timer *>(0));
		}
		CreateTimer(SYSTEM_CLOCK);
		myClocks[SYSTEM_CLOCK]->SetPauseState(false);
		CreateTimer(MASTER_CLOCK);
		myClocks[MASTER_CLOCK]->SetPauseState(false);
		mySystemClock->AttachTimer(myClocks[SYSTEM_CLOCK]);
		mySystemClock->AttachTimer(myClocks[MASTER_CLOCK]);
	}

	void TimeManager::CreateTimer(const Clocks &aTimer)
	{
		if(myClocks[aTimer] != 0)
		{
			delete myClocks[aTimer];
			myClocks[aTimer] = 0;
		}
		myClocks[aTimer] = new Timer(aTimer);
	}

	bool TimeManager::AttachTimerToTimer(const Clocks &aTimerToAttach)
	{
		return AttachTimerToTimer(aTimerToAttach, myDefaultClock);
	}

	bool TimeManager::AttachTimerToTimer(const Clocks &aTimerToAttach, const Clocks &aTimerToAttachTo)
	{
		if((myClocks[aTimerToAttachTo] != 0) && (myClocks[aTimerToAttach] != 0))
		{
			myClocks[aTimerToAttachTo]->AttachTimer(myClocks[aTimerToAttach]);
			return true;
		}
		return false;
	}

	Time TimeManager::GetElapsedTime() const
	{
		if(myClocks[myDefaultClock] != 0)
		{
			return myClocks[myDefaultClock]->GetElapsedTime();
		}
		return Time(myDefaultClock, 0);
	}

	Time TimeManager::GetTotalTime() const
	{
		if(myClocks[myDefaultClock] != 0)
		{
			return myClocks[myDefaultClock]->GetTotalTime();
		}
		return Time(myDefaultClock, 0);
	}

	Time TimeManager::GetElapsedTime(const Clocks &aTimer) const
	{
		if(myClocks[aTimer] != 0)
		{
			return myClocks[aTimer]->GetElapsedTime();
		}
		return Time(aTimer, 0);
	}

	Time TimeManager::GetTotalTime(const Clocks &aTimer) const
	{
		if(myClocks[aTimer] != 0)
		{
			return myClocks[aTimer]->GetTotalTime();
		}
		return Time(aTimer, 0);
	}

	bool TimeManager::SetClockPauseState(const Clocks &aTimer,const bool &aPauseState)
	{
		if(aTimer == ANY_CLOCK)
		{
			for(unsigned int timerIndex=MASTER_CLOCK; timerIndex<myClocks.size(); timerIndex++)
			{
				if(myClocks[timerIndex] != 0)
				{
					myClocks[timerIndex]->SetPauseState(aPauseState);
				}
			}
			return false;
		}
		else if(myClocks[aTimer] != 0)
		{
			return myClocks[aTimer]->SetPauseState(aPauseState);
		}
		return true;
	}

	bool TimeManager::GetClockPauseState(const Clocks &aTimer) const
	{
		if(myClocks[aTimer] != 0)
		{
			return myClocks[aTimer]->GetPauseState();
		}
		return false;
	}

	void TimeManager::SetCurrentDefaultClock(const Clocks &aTimer)
	{
		myDefaultClock = aTimer;
	}

	void TimeManager::Update()
	{
		mySystemClock->Update();
	}
};
